let app = getApp();
let QQMapWX = require("./qqmap-wx-jssdk1.2/qqmap-wx-jssdk.min.js");
// 实例化API核心类
let demo = new QQMapWX({
  key: 'MZWBZ-DVJWP-SKQDN-VTB4S-YEVAT-XNFLR'
});
/**
 * 通过GPS获取详细位置
 */
function getGpsLocation(lat, lon) {
  return new Promise(function (resolve, reject) {
      analyzeGps(lat, lon).then((res) => {
        resolve(res);
      });
  })
}
/**
 * 解析GPS
 */
function analyzeGps(lat, lon) {
  return new Promise(function (resolve, reject) {
    demo.reverseGeocoder({
      //腾讯地图api 逆解析方法 首先设置经纬度
      location: {
        latitude: lat,
        longitude: lon
      }, //逆解析成功回调函数
      success: function (res) {
        console.log(res)
        let address_component = res.result.address_component || {};
        let address_reference = res.result.address_reference;
        address_component.town = address_reference.town.title || '';
        resolve(address_component);
      }
    })
  })
}
// 定位授权
function toSetting() {
  wx.openSetting({
    success(res) {
      console.log(res.authSetting)
      if (res.authSetting["scope.userLocation"]) {
        wx.getLocation({
          type: 'wgs84',
          success(res) {
            const latitude = res.latitude
            const longitude = res.longitude
            const speed = res.speed
            const accuracy = res.accuracy
          }
        })
      }
    }
  })
}
module.exports = {
  getGpsLocation,
  analyzeGps,
  toSetting
}